import json
from EdgeGPT import Chatbot, ConversationStyle
from utils.wechart import wechat
import re

class EdgeGptOperator:
    def __init__(self):
        with open('cookies.json', 'r') as f:
            cookies = json.load(f)
        self.bot = Chatbot(cookies=cookies)
        
    # 连续对话
    async def bingAnswerSend(self,prompt):
        bot = self.bot
        #  creative 有创造力的、 precise 准确的、 balanced 平衡的
        reply = await bot.ask(prompt, conversation_style=ConversationStyle.precise,wss_link="wss://sydney.bing.com/sydney/ChatHub")
        # get reply text
        text = f"{reply['item']['messages'][1]['text']}"
        superscript_map = {'0': '⁰', '1': '¹', '2': '²', '3': '³', '4': '⁴', '5': '⁵', '6': '⁶', '7': '⁷', '8': '⁸', '9': '⁹'}
        text = re.sub(r'\[\^(\d+)\^\]', lambda match: ''.join(superscript_map.get(digit, digit) for digit in match.group(1)), text)
        # await bot.close()
        print("回答：" + text)
        wechat.send_text_message(text)
        return text
    

    # 重置会话
    async def resetBot(self):
        bot = self.bot
        #  重置会话
        await bot.reset()
        text = "重置会话完成"
        wechat.send_text_message(text)
        return text
        
edgeGptOperator = EdgeGptOperator()
